package com.whc.gulimall.order.controller;

import cn.hutool.core.lang.UUID;
import com.whc.common.utils.PageUtils;
import com.whc.common.utils.R;
import com.whc.gulimall.order.entity.OrderEntity;
import com.whc.gulimall.order.entity.OrderReturnReasonEntity;
import com.whc.gulimall.order.service.OrderService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;


/**
 * 订单
 *
 * @author wanghaochen
 * @email angaoen@outlook.com
 * @date 2022-03-05 00:16:47
 */
@RestController
@RequestMapping("order/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    public String sendOrder(@RequestParam(name = "num", defaultValue = "5") Integer num) {
        for (int i = 0; i < num; i++) {

            OrderReturnReasonEntity entity = new OrderReturnReasonEntity();
            entity.setId((long) i);
            rabbitTemplate.convertAndSend("gulimall-exchange", "gulimall.queue",
                    entity, new CorrelationData(UUID.fastUUID().toString()));

        }
        return "SUCCESS";
    }

    @PostMapping("/order")
    public R listOrderItem(@RequestBody Map<String, Object> params) {
        return R.ok().setData(orderService.listOrderItem(params));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("order:order:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("order:order:info")
    public R info(@PathVariable("id") Long id) {
        OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("order:order:save")
    public R save(@RequestBody OrderEntity order) {
        orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("order:order:update")
    public R update(@RequestBody OrderEntity order) {
        orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("order:order:delete")
    public R delete(@RequestBody Long[] ids) {
        orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 通过orderSN获取订单状态
     *
     * @param orderSN orderSN
     * @return Integer
     */
    @GetMapping("/status")
    public R getOrderStatusByOrderSN(@RequestParam("orderSN") String orderSN) {
        Integer status = orderService.getOrderStatusByOrderSN(orderSN);
        return R.ok().setData(status);
    }


}
